package com.captain.special.二叉树.中等;

import com.captain.leetcode.TreeNode;
import com.captain.leetcode.链表.ListNode;

import java.util.ArrayList;
import java.util.List;

/**
 * Des:
 *
 * @author XL
 * @Date 2022/5/18 17:43
 */
public class 二叉树展开为链表114 {
    public void flatten(TreeNode root) {
        if (root == null) return;
        List<TreeNode> ans = new ArrayList<>();
        preRecur(root, ans);
        int size = ans.size();
        for (int i = 1; i < size; i++) {
            TreeNode prev = ans.get(i - 1), curr = ans.get(i);
            prev.left = null;
            prev.right = curr;
        }
    }

    public void preRecur(TreeNode node, List<TreeNode> ans) {
        if (node == null) return;
        ans.add(node);
        preRecur(node.left, ans);
        preRecur(node.right, ans);
    }
}
